<?php
namespace app\admin\controller;

class Admin extends Base{
    //
    protected function initialize() {
        parent::initialize();
        $this->model = 'Admin';
    }
    //
    protected function _before_index(){
        $thead = [
            'aid' => 'AID',
            'username' =>'登录帐号',
            'nickname' =>'账号昵称',
            'group' =>'权限群组',
            'status' =>'使用状态',
            'desc' =>'备注信息',
            'addtime' =>'创建时间',
        ];
        $this->assign('thead', $thead);
    }
    //
    protected function _after_list($list){
        foreach($list as $key => $row){
            $list[$key]['status'] = $row['status'] == 0 ? '冻结' : '正常';
            //$list[$key]['super'] = $row['super'] == 1 ? '是' : '否';
            $list[$key]['addtime'] = date('Y-m-d H:i:s', $row['addtime']);
            $gArr = model('Group')->alias('a')->join('auth_group_access b', 'a.id = b.group_id')->where('b.uid', '=', $row['aid'])->column('a.title');
            $list[$key]['group'] = implode(',', $gArr);
        }
        return $list;
    }
    protected function _before_add(){
        $gArr = model('Group')->where('status', '=', 1)->field(['id', 'title'])->select();
        $this->assign('gArr', $gArr);
    }
    //
    protected function _before_insert($data){
        if(trim($data['password']) == ''){
            $data['password'] = '';
        }else{
            $data['password'] = md6(trim($data['password']));
        }
        $data['gid'] = $data['gid'] ?? [];
        $data['addtime'] = time();
        return $data;
    }
    //
    protected function _after_add($data, $id){
        if($data['gid']){
            foreach($data['gid'] as $val){
                db('auth_group_access')->insert(['uid'=>$id, 'group_id'=>$val]);
            }
        }
        $data['gid'] = serialize($data['gid']);
        $this->log(['pk' => 'aid', 'value' => $id], $data);
    }
    //
    protected function _before_edit(){
        $this->_before_add();
    }
    //
    protected function _before_save($data){
        if(!isset($data['password']) || trim($data['password']) == ''){
            unset($data['password']);
        }else{
            $data['password'] = md6(trim($data['password']));
        }
        if($data['aid'] == 1){
            $data['username'] = 'admin';
        }
        $data['gid'] = $data['gid'] ?? [];
        return $data;
    }
    //
    protected function _filter_edit($list){
        $list['group'] = db('auth_group_access')->where('uid', '=', $list['aid'])->column('group_id');
        return $list;
    }
    //
    protected function _after_edit($data){
        $oArr = db('auth_group_access')->where('uid', '=', $data['aid'])->column('group_id');
        $new = array_diff($data['gid'], $oArr);
        $cut = array_diff($oArr, $data['gid']);
        foreach($new as $val){
            db('auth_group_access')->insert(['uid'=>$data['aid'], 'group_id'=>$val]);
        }
        foreach($cut as $val){
            db('auth_group_access')->where(['uid'=>$data['aid'],'group_id'=>$val])->delete();
        }
        $data['gid'] = serialize($data['gid']);
        $this->log(['pk' => 'aid', 'value' => $data['aid']], $data);
    }
    //
    protected function _filter_del(){
        return [1,2];//系统用户，禁止删除
    }
    //
    protected function _after_del($rows){
        $map = $logs = [];
        foreach($rows as $row){
            $map['uid'][] = $row['aid'];
            $row['gid'] = serialize(db('auth_group_access')->where('uid', '=', $row['aid'])->column('group_id'));
            $logs[] = ['pk' => 'aid', 'value' => $row['aid'], 'row' => $row];
        }
        db('auth_group_access')->where($map)->delete();
        $this->logAll($logs);
    }
    //
    public function resetPassword(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $result = $this->validate($data, 'app\admin\validate\Admin.resetPassword');
            if(true !== $result){
                $return = $this->response($result, false, '', 300);
                return json($return);
            }
            $res = model('Admin')->resetPassword(['password'=>$data['new_password'], 'oldPassword'=>$data['old_password']], $this->userInfo['aid']);
            $res['code'] ? null : $this->log(['pk' => 'aid', 'value' => $this->userInfo['aid'], 'desc' => '修改了密码', 'data' => serialize(['oldPassword'=>$data['old_password']])]);
            $return = $this->response($res['msg'], $res['code'] ? false : true, '', $res['code'] ? 300 : 200);
            return json($return);
        }
        return $this->fetch();
    }
}
